<?php


namespace app\api\controller;

use app\BaseController;
use app\common\JWTToken\TokenUtil;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Guid\Guid;
use think\facade\Cache;

class Authorization extends BaseController
{

    public function index()
    {
        $appId = input('get.appId');
        $code = input('get.code');
        $expTime =(new \DateTimeImmutable())->modify('+6 hour');
        $tokenUtils = new TokenUtil();

        // 生成 UUID
        $uuid = Uuid::uuid4()->toString();
        $refresh_uuid = Uuid::uuid4()->toString();
        $access_token = $tokenUtils->createToken($appId, $code, $expTime);
        $expRefreshTime = (new \DateTimeImmutable())->modify('+720 hour');;
        $refresh_token = $tokenUtils->createToken($appId, $code, $expRefreshTime);
        Cache::set($uuid,$access_token);
        Cache::set($refresh_uuid,$refresh_token);

        $data = ['access_token' => $uuid, 'refresh_token' => $refresh_uuid, 'expires' => 6];


        return json($data);
    }

}